home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / vector4.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  5KB  |  168 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  VECTOR4.H - 4-D Homogeneous Co-ordinates Vector Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. #ifndef _VECTOR4_H
  39. #define _VECTOR4_H
  40.  
  41. #include <math.h>
  42. #include "vector3.h"
  43.  
  44. class ViewSys;  // Forward reference
  45.  
  46. class Vector4 : public Space3   // 4-D vector
  47. {
  48.   private:
  49.     float w;    // W-axis co-ordinate
  50.  
  51.   public:
  52.     Vector4() : Space3() { };
  53.  
  54.     Vector4( double xval, double yval, double zval, double
  55.         wval ) : Space3( xval, yval, zval )
  56.     { w = (float) wval; }
  57.  
  58.     double GetW() { return w; }
  59.     void SetW( double wval ) { w = (float) wval; }
  60.  
  61.     // Return vector length
  62.     double Length()
  63.     { return sqrt(x * x + y * y + z * z + w * w); }
  64.  
  65.     // Normalize vector
  66.     Vector4 &Norm()
  67.     {
  68.       double len = Length();
  69.  
  70.       if (len < MIN_VALUE)
  71.         len = 1.0;
  72.  
  73.       x /= (float) len;
  74.       y /= (float) len;
  75.       z /= (float) len;
  76.       w /= (float) len;
  77.  
  78.       return *this;
  79.     }
  80.  
  81.     // Multiply by scalar s
  82.     Vector4 &operator*=( double s )
  83.     {
  84.       x *= (float) s;
  85.       y *= (float) s;
  86.       z *= (float) s;
  87.       w *= (float) s;
  88.       
  89.       return *this;
  90.     }
  91.  
  92.     // Add vector v2 to vector v1
  93.     friend Vector4 operator+( Vector4 &v1, Vector4 &v2 )
  94.     {
  95.       Vector4 temp;     // Temporary 4-D vector
  96.  
  97.       temp.x = v1.x + v2.x;
  98.       temp.y = v1.y + v2.y;
  99.       temp.z = v1.z + v2.z;
  100.       temp.w = v1.w + v2.w;
  101.  
  102.       return temp;
  103.     }
  104.  
  105.     // Subtract vector v2 from vector v1
  106.     friend Vector4 operator-( Vector4 &v1, Vector4 &v2 )
  107.     {
  108.       Vector4 temp;     // Temporary 4-D vector
  109.  
  110.       temp.x = v1.x - v2.x;
  111.       temp.y = v1.y - v2.y;
  112.       temp.z = v1.z - v2.z;
  113.       temp.w = v1.w - v2.w;
  114.  
  115.       return temp;
  116.     }
  117.  
  118.     // Return dot product of vectors v1 and v2
  119.     friend double Dot( Vector4 &v1, Vector4 &v2 )
  120.     { return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z +
  121.         v1.w * v2.w); }
  122.  
  123.     // Premultiply point by projective matrix
  124.     void ProjTransform( Point3 &p, double (*ptm)[4] )
  125.     {
  126.       x = (float) (ptm[0][0] * p.GetX() + ptm[0][1] *
  127.           p.GetY() + ptm[0][2] * p.GetZ() + ptm[0][3]);
  128.       y = (float) (ptm[1][0] * p.GetX() + ptm[1][1] *
  129.           p.GetY() + ptm[1][2] * p.GetZ() + ptm[1][3]);
  130.       z = (float) (ptm[2][0] * p.GetX() + ptm[2][1] *
  131.           p.GetY() + ptm[2][2] * p.GetZ() + ptm[2][3]);
  132.       w = (float) (ptm[3][0] * p.GetX() + ptm[3][1] *
  133.           p.GetY() + ptm[3][2] * p.GetZ() + ptm[3][3]);
  134.     }
  135.  
  136.     // Premultiply vector by projective matrix
  137.     void ProjTransform( Vector3 &p, double (*ptm)[4] )
  138.     {
  139.       x = (float) (ptm[0][0] * p.GetX() + ptm[0][1] *
  140.           p.GetY() + ptm[0][2] * p.GetZ() + ptm[0][3]);
  141.       y = (float) (ptm[1][0] * p.GetX() + ptm[1][1] *
  142.           p.GetY() + ptm[1][2] * p.GetZ() + ptm[1][3]);
  143.       z = (float) (ptm[2][0] * p.GetX() + ptm[2][1] *
  144.           p.GetY() + ptm[2][2] * p.GetZ() + ptm[2][3]);
  145.       w = (float) (ptm[3][0] * p.GetX() + ptm[3][1] *
  146.           p.GetY() + ptm[3][2] * p.GetZ() + ptm[3][3]);
  147.     }
  148.  
  149.     // Perform perspective division on point
  150.     void Perspective( Point3 *pp )
  151.     {
  152.       pp->SetX(x / w);
  153.       pp->SetY(y / w);
  154.       pp->SetZ(z / w);
  155.     }
  156.  
  157.     // Perform perspective division on vector
  158.     void Perspective( Vector3 *pp )
  159.     {
  160.       pp->SetX(x / w);
  161.       pp->SetY(y / w);
  162.       pp->SetZ(z / w);
  163.     }
  164. };
  165.  
  166. #endif
  167.  
  168.